package com.szeastroc.core.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szeastroc.common.query.BaseQuery;
import com.szeastroc.core.base.service.impl.BaseServiceImpl;
import com.szeastroc.core.bean.system.SystemResource;
import com.szeastroc.core.bean.system.SystemRole;
import com.szeastroc.core.bean.system.SystemUser;
import com.szeastroc.core.bean.system.SystemUserRole;
import com.szeastroc.core.mapper.system.SystemUserMapper;
import com.szeastroc.core.mapper.system.SystemUserRoleMapper;
import com.szeastroc.core.service.system.SystemResourceService;
import com.szeastroc.core.service.system.SystemRoleService;
import com.szeastroc.core.service.system.SystemUserService;

@Service
public class SystemUserServiceImpl extends BaseServiceImpl<SystemUser> implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private SystemResourceService systemResourceService;

    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;

    public SystemUser selectByUserName(String userName) {
        SystemUser user = systemUserMapper.selectByUserName(userName);
        if (user != null) {
            List<SystemRole> roles = systemRoleService.selectRolesByUserId(user.getId());
            user.setRoles(roles);
            StringBuffer roleIds = new StringBuffer();
            for (SystemRole systemRole : roles) {
                roleIds.append(systemRole.getId()).append(",");
            }
            if (roles != null && roles.size() > 0) {
                List<SystemResource> systemResources = systemResourceService.getSystemResourceByRoleIds(roleIds.toString().substring(0, roleIds.length() - 1));
                user.setSystemResource(systemResources);
            }
        }
        return user;
    }

    public PageInfo<SystemUser> selectPage(BaseQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<SystemUser> list = systemUserMapper.selectUsers(query);
        PageInfo<SystemUser> pageInfo = new PageInfo<SystemUser>(list);
        return pageInfo;
    }

    @Transactional
    public void setUserRole(Integer userId, Integer roleId, Integer oldRoleId) {
        if (oldRoleId != null) {
            systemUserRoleMapper.deleteByUserIdAndRoleId(userId, oldRoleId);
        }
        systemUserRoleMapper.insert(new SystemUserRole(userId, roleId));
    }

    public SystemUser selectByNameAndPassword(String userName, String password) {
        return systemUserMapper.selectByNameAndPassword(userName, password);
    }

    @Transactional
    public void deleteUser(Integer userId, Integer roleId) {
        systemUserMapper.deleteByPrimaryKey(userId);
        if(roleId != null){
            systemUserRoleMapper.deleteByUserIdAndRoleId(userId, roleId);
        }
    }

    @Transactional
    public void saveUser(SystemUser user){
        user.setStatus(1);
        systemUserMapper.insertUseGeneratedKeys(user);
        systemUserRoleMapper.insertSelective(new SystemUserRole(user.getId(),user.getRoleId()));
    }
}
